<template>
  <el-dialog :title="dialogProps.title" :visible.sync="dialogProps.visible" width="1000px"
    @open="handleDialogOpen()" v-loading="loading">
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>
    <el-form :model="jbkModel"
      ref="libForm" label-width="80px" label-position="right" size="small" :inline="true" :rules="jbk">
      <el-form-item label="疾病名称" prop="name">
        <el-col :span="16"><el-input  :maxlength="32" v-model="jbkModel.name"></el-input></el-col>
      </el-form-item>
      <el-form-item label="异常类型" prop="exceptionTypes">
        <el-col :span="16">
          <el-select v-model="jbkModel.exceptionTypes">
            <el-option label="系统自带" value="系统自带"></el-option>
              <el-option label="自定义" value="自定义"></el-option>
          </el-select>
          </el-col>
      </el-form-item>
      <el-form-item label="是否职业病" prop="occupationalDisease">
        <el-col :span="16">
           <el-select v-model="jbkModel.occupationalDisease">
              <el-option label="是" value="0"></el-option>
              <el-option label="不是" value="1"></el-option>
          </el-select>
          </el-col>
      </el-form-item>
      <el-form-item label="适用性别" prop="gender">
        <el-col :span="16">
          <el-select v-model="jbkModel.gender">
              <el-option label="男" value="男"></el-option>
              <el-option label="女" value="女"></el-option>
              <el-option label="通用" value="通用"></el-option>
          </el-select>
          </el-col>
      </el-form-item>
      <el-form-item label="就医期限" prop="medicalTreatmentPeriod">
        <el-col :span="16">
          <el-select v-model="jbkModel.medicalTreatmentPeriod">
              <el-option label="立即" value="立即"></el-option>
              <el-option label="半年内" value="半年内"></el-option>
              <el-option label="三个月内" value="三个月内"></el-option>
              <el-option label="一个月内" value="一个月内"></el-option>
              <el-option label="一周内" value="一周内"></el-option>
          </el-select>
          </el-col>
      </el-form-item>
      <el-form-item label="就诊科室" prop="clinicDepartmentId">
        <el-col :span="16">
          <el-select v-model="jbkModel.clinicDepartmentId">
               <el-option
                    v-for="item in TClinicDepartmentList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id">
                </el-option>
          </el-select>
          </el-col>
      </el-form-item>
      <el-form-item label="预警等级" prop="warningLevel">
        <el-col :span="16">
          <el-select v-model="jbkModel.warningLevel">
              <el-option label="一级" value="一级"></el-option>
              <el-option label="二级" value="二级"></el-option>
              <el-option label="三级" value="三级"></el-option>
              <el-option label="四级" value="四级"></el-option>
          </el-select>
          </el-col>
      </el-form-item>
      <el-form-item label="其他名称" prop="otherName">
         <el-input type="textarea" v-model="jbkModel.otherName" style="width:700px"></el-input>
      </el-form-item>
      <el-form-item label="医学解释" prop="medicalInterpretation">
         <el-input type="textarea" v-model="jbkModel.medicalInterpretation" style="width:700px"></el-input>
      </el-form-item>
      <el-form-item label="常见原因" prop="reason">
         <el-input type="textarea" v-model="jbkModel.reason" style="width:700px"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
      <el-button type="primary" size="small" @click="submitForm('libForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {ThealthplanAdd,Thealthplanupd} from '@/api/jibingku_tj'
export default {
  name: 'jbk-edit',
  props:['TClinicDepartmentList'],
  data() {
    return {
      loading: false,
      jbkModel: {

      },
      dialogProps: {
        visible: false,
        action: '',
        title: ''
      },
      jbk:{
      name: [
            { required: true, message: '请输入疾病名称', trigger: 'blur' },
            // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
      exceptionTypes:[
           { required: true, message: '请选择异常类型', trigger: 'blur' },
            // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
      ],
      occupationalDisease:[
           { required: true, message: '请选择职业病', trigger: 'blur' },
            // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
      ],
      gender:[
           { required: true, message: '请选择适用性别', trigger: 'blur' },
            // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
      ],
      medicalTreatmentPeriod:[
           { required: true, message: '请选择就医期限', trigger: 'blur' },
            // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
      ],
      warningLevel:[
           { required: true, message: '请选择预警等级', trigger: 'blur' },
            // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
      ],
      otherName:[
           { required: true, message: '请输入其他名称', trigger: 'blur' },
            // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
      ],
      medicalInterpretation:[
           { required: true, message: '请输入医学解释', trigger: 'blur' },
            // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
      ],
       reason:[
           { required: true, message: '请输入常见原因', trigger: 'blur' },
            // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
      ],
      clinicDepartmentId:[
        { required: true, message: '请选择就诊科室', trigger: 'blur' },
      ]

      },
      }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if(this.dialogProps.action === 'add') {
            this.doSaveTmeals()
          } else {
            this.doUpdatessk()
          }
        } else {
          return false
        }
      });
    },

    doUpdatessk() {
      this.loading = true
      Thealthplanupd(this.jbkModel)
      .then(response => {
        this.loading = false
        this.dialogProps.visible = false
        this.$emit('save-finished')
      })
      .catch(error => {
        this.outputError(error)
      })
    },
    doSaveTmeals() {
      this.loading = true
      ThealthplanAdd(this.jbkModel)
      .then(response => {
        this.loading = false
        this.dialogProps.visible = false
        this.$emit('save-finished')
      })
      .catch(error => {
        this.outputError(error)
      })
    },
    handleDialogClose() {
      this.dialogProps.visible = false
    },
    handleDialogOpen() {
      this.$nextTick(() => {
        this.$refs['libForm'].clearValidate()
      })
    },
    outputError(error) {
      console.log(error.response ? error.response : error.message)
      this.loading = false
      this.$message({
        showClose: true,
        message: '出错了，请按F12查看浏览器日志。',
        type: 'error'
      })
    }
  },
  mounted: function() {
    this.$nextTick(() => {
      this.$on('openEditDialog', function(TDiseaseLibrary) {
        this.jbkModel=TDiseaseLibrary
        console.log(this.jbkModel)
        this.dialogProps.action = 'edit'
        this.dialogProps.title = '修改'
        this.dialogProps.visible = true
      })
      this.$on('openAddDialog', function() {
        this.dialogProps.action = 'add'
        this.dialogProps.title = '添加'
        this.dialogProps.visible = true
      })
    })
  }
}
</script>

